using System;
using System.Windows.Forms;

namespace Nuwanda.TfsSpotlight.Shell
{
    public class ControlInvoker
    {
        private readonly Control _target;

        public ControlInvoker(Control target)
        {
            this._target = target;
        }

        public void RunInControlsThread(Action action)
        {
            if (this._target.InvokeRequired)
            {
                this._target.Invoke(action);
            }
            else
            {
                action();
            }
        }

        public T RunAndReturnInControlsThread<T>(Func<T> action)
        {
            if (this._target.InvokeRequired)
            {
                return (T)this._target.Invoke(action);
            }

            return action();
        }
    }
}